package dao

import (
	"gorm.io/gorm"
	"srv/config"
	"time"
)

// 医生表
type Doctor struct {
	gorm.Model
	Name          string  `gorm:"type:varchar(30);not null;comment:'医生姓名'"`
	Image         string  `gorm:"type:varchar(100);default:'';comment:'头像'"`
	Hospital      string  `gorm:"type:varchar(50);comment:'院区'"`
	Room          string  `gorm:"type:varchar(30);comment:'科室'"`
	NickName      string  `gorm:"type:varchar(30);not null;comment:'昵称'"`
	Type          int8    `gorm:"type:tinyint(1);default:0;comment:'标签:0-专家,1-主任'"`
	RegisterPrice float64 `gorm:"type:decimal(10,2);comment:'挂号费'"`
	InspectPrice  float64 `gorm:"type:decimal(10,2);comment:'诊查费'"`
	Intro         string  `gorm:"type:varchar(255);default:'';comment:'简介'"`
}

// 预约信息表
type Message struct {
	gorm.Model
	Name            string  `gorm:"type:varchar(30);not null;comment:'患者姓名'"`
	IdCardType      int8    `gorm:"type:tinyint(1);default:0;comment:'证件类型:0-居民身份证,1-港澳台身份证'"`
	WXName          string  `gorm:"type:varchar(30);comment:'微信名称'"`
	Address         string  `gorm:"type:varchar(100);not null;comment:'就诊人地址'"`
	MedicalIdCard   string  `gorm:"type:varchar(20);not null;comment:'就诊卡号'"`
	IdCard          string  `gorm:"type:char(18);not null;comment:'身份证号'"`
	Mobile          string  `gorm:"type:char(11);not null;comment:'手机号'"`
	Image           string  `gorm:"type:varchar(100);default:'';comment:'头像'"`
	Relationship    int8    `gorm:"type:tinyint(1);default:0;comment:'关系:0-本人,1-父母,2-子女'"`
	Hospital        string  `gorm:"type:varchar(50);comment:'就诊医院'"`
	HospitalAddress string  `gorm:"type:varchar(100);comment:'医院地址'"`
	Room            string  `gorm:"type:varchar(30);comment:'就诊科室'"`
	DoctorName      string  `gorm:"type:varchar(30);not null;comment:'医生姓名'"`
	NickName        string  `gorm:"type:varchar(30);not null;comment:'医生昵称'"`
	Type            int8    `gorm:"type:tinyint(1);default:0;comment:'挂号类型:0-专家,1-主任'"`
	Time            string  `gorm:"type:varchar(100);comment:'门诊时间'"`
	WaitTime        string  `gorm:"type:varchar(100);comment:'候诊时间'"`
	RegisterPrice   float64 `gorm:"type:decimal(10,2);comment:'挂号费'"`
	CreateTime      string  `gorm:"type:varchar(100);comment:'预约时间'"`
	Status          int8    `gorm:"type:tinyint(1);default:0;comment:'就诊状态:0-等待,1-已取消,2-已完成'"`
	Cancel          string  `gorm:"type:varchar(100);default:'';comment:'取消原因'"`
	CancelTime      string  `gorm:"type:varchar(100);default:'';comment:'取消时间'"`
	Comment         string  `gorm:"type:varchar(255);default:'';comment:'备注'"`
}

// 医生注册
func (d *Doctor) DoctorRegister() (*Doctor, string) {
	err := config.DB.Create(&d).Error
	if err != nil {
		return d, "医生注册失败"
	} else {
		return d, ""
	}
}

// 医生展示
func (d *Doctor) DoctorList() ([]Doctor, string) {
	var dd []Doctor
	err := config.DB.Find(&dd).Error
	if err != nil {
		return nil, "查询失败"
	} else {
		return dd, ""
	}
}

// 预约挂号
func (m *Message) MessageCreate() (uint, string) {
	err := config.DB.Create(&m).Error
	if err != nil {
		return 0, "预约失败"
	} else {
		return m.ID, ""
	}
}

// 查看预约信息
func (m *Message) MessageShow(id int64) (Message, string) {
	var mm Message
	config.DB.Where("id=?", id).First(&mm)
	if mm.ID == 0 {
		return mm, "信息不存在"
	}
	return mm, ""
}
func (m *Message) MessageUpdate(cancel string, id int64) string {
	if cancel != "" {
		var mm Message
		config.DB.Where("id=?", id).First(&mm)
		mm.Cancel = cancel
		mm.CancelTime = time.Now().Format(time.DateTime)
		mm.Status = 1
		err := config.DB.Updates(&mm).Error
		if err != nil {
			return "修改失败"
		}
		return ""
	}
	return "不能没有取消原因"
}
